Skip to content

refactor(config): add shared _resolve_component utility for plugin loading#5215

Open
MikeGoldsmith wants to merge 3 commits into
open-telemetry:mainfrom
MikeGoldsmith:mike/config-shared-resolve-component
Open

refactor(config): add shared _resolve_component utility for plugin loading#5215
MikeGoldsmith wants to merge 3 commits into
open-telemetry:mainfrom
MikeGoldsmith:mike/config-shared-resolve-component

Conversation

@MikeGoldsmith
Copy link
Copy Markdown
Member

Description

Adds a shared _resolve_component() utility in _common.py that extracts the common pattern used by exporter factory functions for plugin loading.

Suggested by @lzchen in #5098.

The pattern

All three exporter factory functions (_create_span_exporter, _create_log_record_exporter, _create_push_metric_exporter) follow the same structure:

  1. Iterate a registry of built-in names → call factory with config value
  2. Check additional_properties for plugin names → load_entry_point(group, name)(**(config or {}))
  3. Raise ConfigurationError if nothing matched

The utility

def _resolve_component(config, registry, entry_point_group, component_type):
    for name, factory in registry.items():
        value = getattr(config, name, None)
        if value is not None:
            return factory(value)
    if config.additional_properties:
        name, plugin_config = next(iter(config.additional_properties.items()))
        return load_entry_point(entry_point_group, name)(**(plugin_config or {}))
    raise ConfigurationError(f"No {component_type} type specified in config.")

Tests

5 tests covering: built-in resolution, plugin loading with config, plugin with empty config, no-component error, plugin-not-found error.

Draft — this PR adds the utility and tests only. The pending exporter PR (#5128) will be updated to use it once both land. The sampler, propagator, and resource detector have enough structural differences that they don't benefit from this utility.

Extracts the common pattern used by exporter factory functions:
check built-in registry → fall back to additional_properties →
load via entry point → raise if nothing matched.

The pending exporter PR (open-telemetry#5128) will be updated to use this utility,
reducing the three near-identical factory functions to one-liners.

Assisted-by: Claude Opus 4.6
@MikeGoldsmith MikeGoldsmith marked this pull request as ready for review May 18, 2026 14:26
@MikeGoldsmith MikeGoldsmith requested a review from a team as a code owner May 18, 2026 14:26
@MikeGoldsmith MikeGoldsmith moved this to Ready for review in Python PR digest May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Ready for review

Development

Successfully merging this pull request may close these issues.

1 participant